package com.gzlz.budejie.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gzlz.budejie.R;
import com.gzlz.budejie.bean.User;
import com.gzlz.helper.DatabaseHelper;
import com.gzlz.helper.UserService;

/*
 * 登陆界面
 * */
public class LoginActivity extends Activity implements OnClickListener {

	private ImageButton ib_login_back;
	private TextView tv_login_account;
	private LinearLayout ll_zhuce_content;
	private TextView tv_zhuce_content;
	private ImageButton ib_zhuce_back;

	private EditText et_login_number;
	private EditText et_login_password;
	private Button btn_login;
	private Button btn_zhuce;
	private EditText et_zhuce_number;
	private EditText et_zhuce_password;
	
	private DatabaseHelper dbhepler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		dbhepler = new DatabaseHelper(getBaseContext());
		dbhepler.getReadableDatabase();

		initDenglu();

		onClickListener();
	}

	private void initDenglu() {
		ib_login_back = (ImageButton) findViewById(R.id.ib_login_back);
		tv_login_account = (TextView) findViewById(R.id.tv_login_account);
		et_login_number = (EditText) findViewById(R.id.et_login_number);
		et_login_password = (EditText) findViewById(R.id.et_login_password);
		btn_login = (Button) findViewById(R.id.btn_login);
	}

	private void initZhuce() {
		tv_zhuce_content = (TextView) findViewById(R.id.tv_zhuce_account);
		ib_zhuce_back = (ImageButton) findViewById(R.id.ib_zhuce_back);
		et_zhuce_number = (EditText) findViewById(R.id.et_zhuce_number);
		et_zhuce_password = (EditText) findViewById(R.id.et_zhuce_password);
		btn_zhuce = (Button) findViewById(R.id.btn_zhuce);
	}

	// 设置点击事件
	private void onClickListener() {
		// 设置点击返回
		ib_login_back.setOnClickListener(this);
		// 设置账户注册点击事件
		tv_login_account.setOnClickListener(this);
		// 登录的点击事件
		btn_login.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_login_back:
			finish();
			break;
		case R.id.tv_login_account:
			setContentView(R.layout.zhuce_item);
			initZhuce();
			tv_zhuce_content.setOnClickListener(this);
			ib_zhuce_back.setOnClickListener(this);
			btn_zhuce.setOnClickListener(this);// 注册点击事件
			break;
		case R.id.tv_zhuce_account:
			setContentView(R.layout.activity_login);
			initDenglu();
			onClickListener();
			break;
		case R.id.ib_zhuce_back:
			finish();
			break;
		case R.id.btn_login:
			logining();
			break;
		case R.id.btn_zhuce:
			zhuce();
			break;

		default:
			break;
		}
	}

	// 注册
	private void zhuce() {
		UserService uService = new UserService(getBaseContext());
		User user = new User();
		String number = et_zhuce_number.getText().toString();
		String password = et_zhuce_password.getText().toString();
		if ("".equals(number)) {
			Log.e("TAG", "注册按钮。。。");
			Toast.makeText(LoginActivity.this, "手机号码不能为空", 0).show();
		} else if (number.length() != 11) {
			Toast.makeText(getApplicationContext(), "手机号码格式不正确", 0).show();
		} else if ("".equals(password)) {
			Toast.makeText(getApplicationContext(), "密码不能为空", 0).show();
		} else if (password.length() < 6) {
			Toast.makeText(getApplicationContext(), "密码格式不正确", 0).show();
		}else {
			
			user.setUsername(number);
			user.setPassword(password);
			Log.e("TAG", "number=="+number+"password=="+password);
			uService.register(user);
		}
	}

	// 登录
	private void logining() {
		UserService uService = new UserService(this.getBaseContext());
		// 得到输入的手机号和密码
		String number = et_login_number.getText().toString();
		String password = et_login_password.getText().toString();
		if ("".equals(number)) {
			Log.e("TAG", "登录按钮。。。");
			Toast.makeText(LoginActivity.this, "手机号码不能为空", 0).show();
		} else if (number.length() != 11) {
			Toast.makeText(getApplicationContext(), "手机号码格式不正确", 0).show();
		} else if ("".equals(password)) {
			Toast.makeText(getApplicationContext(), "密码不能为空", 0).show();
		} else if (password.length() < 6) {
			Toast.makeText(getApplicationContext(), "密码格式不正确", 0).show();
		} else {
			 boolean flag=uService.login(number, password);
		        if(flag){
		            Log.i("TAG","登录成功");
		        }else{
		            Log.i("TAG","登录失败,账户名或密码错误");
		        }
			
		}
	}

}
